//https://leetcode.cn/problems/number-of-2s-in-range-lcci/
class Solution {
public:
    int numberOf2sInRange(int n) {
        int res = 0;
        for (long i = 1; i <= n; i *= 10) {
            int temp = n / i;
            int cur = temp % 10;
            int temp1 = temp / 10;
            int temp2 = n % i;
            res += temp1 * i;
            if (cur == 2) res += temp2 + 1;
            if (cur > 2)  res += i;
        }
        return res;
    }
};
